from tortoise.models import Model
from tortoise import fields
from enum import Enum

# 定义枚举类
class ProductTypeEnum(str, Enum):
    COFFEE = "coffee"
    MERCHANDISE = "merchandise"

class RelationTypeEnum(str, Enum):
    COMPLEMENTARY = "complementary"
    ALTERNATIVE = "alternative"
    ACCESSORY = "accessory"
    BUNDLE = "bundle"


class User(Model):
    """用户表"""
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=50, unique=True)
    email = fields.CharField(max_length=100, unique=True)
    password_hash = fields.CharField(max_length=255, description="密码的哈希值，非明文")
    head_image = fields.CharField(max_length=255, null=True)
    phone = fields.CharField(max_length=20, null=True)
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)
    last_login = fields.DatetimeField(null=True)
    is_active = fields.BooleanField(default=True)

    class Meta:
        table = "users"

    def __str__(self):
        return self.username


class UserAddress(Model):
    """用户地址表"""
    id = fields.IntField(pk=True)
    user = fields.ForeignKeyField("models.User", related_name="addresses")
    recipient_name = fields.CharField(max_length=50)
    phone = fields.CharField(max_length=20)
    province = fields.CharField(max_length=50)
    city = fields.CharField(max_length=50)
    county = fields.CharField(max_length=50)
    detail_address = fields.CharField(max_length=255)
    is_default = fields.BooleanField(default=False)
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "user_addresses"


class CoffeeCategory(Model):
    """咖啡豆分类表"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50)
    description = fields.TextField(null=True)
    beantype_image = fields.CharField(max_length=255, null=True)
    parent = fields.ForeignKeyField("models.CoffeeCategory", null=True, related_name="children")
    sort_order = fields.IntField(default=0)
    is_active = fields.BooleanField(default=True)

    class Meta:
        table = "coffee_categories"


class CoffeeProduct(Model):
    """咖啡豆产品表"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=100)
    category = fields.ForeignKeyField("models.CoffeeCategory", related_name="products")
    origin_region = fields.CharField(max_length=100)
    flavor_notes = fields.TextField(null=True)
    description = fields.TextField(null=True)
    price = fields.DecimalField(max_digits=10, decimal_places=2)
    original_price = fields.DecimalField(max_digits=10, decimal_places=2, null=True)
    stock = fields.IntField(default=0)
    weight = fields.DecimalField(max_digits=6, decimal_places=2, null=True)
    image_urls = fields.JSONField(null=True)
    is_recommend = fields.BooleanField(default=False)
    is_puton = fields.BooleanField(default=True)
    sales_count = fields.IntField(default=0)
    view_count = fields.IntField(default=0)
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

    class Meta:
        table = "coffee_products"


class MerchandiseCategory(Model):
    """周边商品分类表"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50, description="分类名称")
    description = fields.TextField(null=True, description="分类描述")
    image_url = fields.CharField(max_length=255, null=True, description="分类图片URL")
    parent = fields.ForeignKeyField("models.MerchandiseCategory", null=True, related_name="children")
    sort_order = fields.IntField(default=0, description="排序序号")
    is_active = fields.BooleanField(default=True, description="是否激活")
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

    class Meta:
        table = "merchandise_categories"


class MerchandiseProduct(Model):
    """周边商品表"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=100, description="商品名称")
    category = fields.ForeignKeyField("models.MerchandiseCategory", related_name="products", description="分类ID")
    description = fields.TextField(null=True, description="商品描述")
    price = fields.DecimalField(max_digits=10, decimal_places=2, description="价格")
    original_price = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="原价")
    stock = fields.IntField(default=0, description="库存数量")
    image_urls = fields.JSONField(null=True, description="图片URL数组")
    is_featured = fields.BooleanField(default=False, description="是否推荐")
    is_active = fields.BooleanField(default=True, description="是否上架")
    sales_count = fields.IntField(default=0, description="销量")
    view_count = fields.IntField(default=0, description="浏览量")
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

    class Meta:
        table = "merchandise_products"


class ProductTag(Model):
    """产品标签表"""
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50, unique=True, description="标签名称")
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "product_tags"


class CoffeeProductTag(Model):
    """咖啡豆-标签关联表"""
    id = fields.IntField(pk=True)
    product = fields.ForeignKeyField("models.CoffeeProduct", related_name="tag_relations")
    tag = fields.ForeignKeyField("models.ProductTag", related_name="coffee_products")
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "coffee_product_tags"
        unique_together = (("product", "tag"),)


class MerchandiseProductTag(Model):
    """周边商品-标签关联表"""
    id = fields.IntField(pk=True)
    product = fields.ForeignKeyField("models.MerchandiseProduct", related_name="tag_relations")
    tag = fields.ForeignKeyField("models.ProductTag", related_name="merchandise_products")
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "merchandise_product_tags"
        unique_together = (("product", "tag"),)


class ProductRelation(Model):
    """产品关联表"""
    id = fields.IntField(pk=True)
    main_product_id = fields.IntField(description="主产品ID")
    main_product_type = fields.CharEnumField(
        enum_type=ProductTypeEnum,
        description="主产品类型"
    )
    related_product_id = fields.IntField(description="关联产品ID")
    related_product_type = fields.CharEnumField(
        enum_type=ProductTypeEnum,
        description="关联产品类型"
    )
    relation_type = fields.CharEnumField(
        enum_type=RelationTypeEnum,
        default=RelationTypeEnum.COMPLEMENTARY,
        description="关联类型"
    )
    sort_order = fields.IntField(default=0, description="排序顺序")
    is_active = fields.BooleanField(default=True, description="是否激活")
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

    class Meta:
        table = "product_relations"
        unique_together = (("main_product_id", "main_product_type", "related_product_id", "related_product_type"),)


class CartItem(Model):
    """购物车表"""
    id = fields.IntField(pk=True)
    user = fields.ForeignKeyField("models.User", related_name="cart_items")
    product = fields.ForeignKeyField("models.CoffeeProduct", related_name="cart_items")
    quantity = fields.IntField(default=1)
    added_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "cart_items"


class Order(Model):
    """订单表"""
    id = fields.IntField(pk=True)
    order_number = fields.CharField(max_length=50, unique=True)
    user = fields.ForeignKeyField("models.User", related_name="orders")
    total_amount = fields.DecimalField(max_digits=10, decimal_places=2)
    status = fields.CharField(max_length=20, default="pending")
    address = fields.ForeignKeyField("models.UserAddress", related_name="orders")
    payment_method = fields.CharField(max_length=20, null=True)
    payment_status = fields.CharField(max_length=20, default="pending")
    refund_status = fields.CharField(max_length=20, default="none", description="退款状态")
    payment_at = fields.DatetimeField(null=True, description="支付成功的时间")
    remark = fields.TextField(null=True, description="订单备注")
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

    class Meta:
        table = "orders"


class OrderItem(Model):
    """订单项表"""
    id = fields.IntField(pk=True)
    order = fields.ForeignKeyField("models.Order", related_name="items")
    product_id = fields.IntField()  # 可以是咖啡豆或周边商品的ID
    product_type = fields.CharEnumField(
        enum_type=ProductTypeEnum,
        default=ProductTypeEnum.COFFEE,
        description="产品类型"
    )
    product_name = fields.CharField(max_length=100)
    product_image = fields.CharField(max_length=255, null=True)
    price = fields.DecimalField(max_digits=10, decimal_places=2)
    quantity = fields.IntField()
    total_price = fields.DecimalField(max_digits=10, decimal_places=2)

    class Meta:
        table = "order_items"


class Favorite(Model):
    """收藏表"""
    id = fields.IntField(pk=True)
    user = fields.ForeignKeyField("models.User", related_name="favorites")
    product_id = fields.IntField()  # 可以是咖啡豆或周边商品的ID
    product_type = fields.CharEnumField(
        enum_type=ProductTypeEnum,
        default=ProductTypeEnum.COFFEE,
        description="产品类型"
    )
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "favorites"
        unique_together = (("user", "product_id", "product_type"),)


class ProductReview(Model):
    """产品评论表"""
    id = fields.IntField(pk=True)
    user = fields.ForeignKeyField("models.User", related_name="reviews")
    product_id = fields.IntField()  # 可以是咖啡豆或周边商品的ID
    product_type = fields.CharEnumField(
        enum_type=ProductTypeEnum,
        default=ProductTypeEnum.COFFEE,
        description="产品类型"
    )
    order = fields.ForeignKeyField("models.Order", null=True, related_name="reviews")
    rating = fields.IntField()  # 评分(1-5)
    content = fields.TextField(null=True)
    image_urls = fields.JSONField(null=True, description="用户上传的评论图片URL数组")
    is_verified = fields.BooleanField(default=False, description="是否已验证购买")
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "product_reviews"